﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Data;
using System.Data.Entity;
using System.ComponentModel.DataAnnotations;

namespace HdipCricketClub.Models
{
    [MetadataType(typeof(PlayerSeasonStatisticsMetaData))]
    public partial class PlayerSeasonStatistic
    {
        CricketClubEntities db = new CricketClubEntities();
        
        public double BattingAverage
        {
            get
            {
                if ((batting_runs == null) || (innings == null))
                {
                    return 0;
                }

                if ((batting_runs != null) && (innings != null) && (not_out == null))
                {
                    return Math.Round((double)batting_runs / (double)innings, 2);
                }

                if ((innings - not_out) == 0)
                {
                    return 0;
                }
                else
                {
                    return Math.Round((double)batting_runs / ((double)innings - (double)not_out), 2);
                }
            }
        }

        public double BowlingAverage
        {
            get
            {
                if ((wickets != null) && (bowling_runs != null))
                {
                    return Math.Round((double)bowling_runs / (double)wickets, 2);
                }
                else
                {
                    return 0;
                }
            }
        }


        public double RunsPerOver
        {
            get
            {
                if ((bowling_runs != null) && (overs != null))
                {
                    return Math.Round((double)bowling_runs / (double)overs, 2);
                }
                else
                {
                    return 0;
                }
            }
        }

        public void UpdateClubPlayerSeasonStatistics(FormCollection scorecard, Fixture fixture)
        {
            var currentFixturePlayersToUpdate = db.ClubFixturePlayers.Where(cfp => cfp.fixture_id == fixture.fixture_id).ToList();

            IQueryable<int?> currentFixturePlayersToUpdateIds = currentFixturePlayersToUpdate.Select(cfp => cfp.player_id).AsQueryable();

            IQueryable<int> statisticsPlayers = db.PlayerSeasonStatistics.Select(sp => sp.player_id).AsQueryable();

            foreach (int p in currentFixturePlayersToUpdateIds)
            {
                if (statisticsPlayers.Contains(p))
                {
                    PlayerSeasonStatistic currentPlayerToUpdate = db.PlayerSeasonStatistics.Where(cp => cp.player_id == p).Single();
                    ClubFixturePlayer currentFixturePlayerToUpdate = db.ClubFixturePlayers.Where(cfp => cfp.fixture_id == fixture.fixture_id && cfp.player_id == p).Single();

                    currentPlayerToUpdate.matches += 1;

                    if (currentFixturePlayerToUpdate.how_out != 8)                          // Dismissal Id 8 = did not bat
                    {
                        currentPlayerToUpdate.innings += 1;
                    }

                    if (currentFixturePlayerToUpdate.how_out == 9)                          // Dismissal id 9 = not out
                    {
                        currentPlayerToUpdate.not_out += 1;
                    }

                    if (currentFixturePlayerToUpdate.batting_runs != null)
                    {
                        currentPlayerToUpdate.batting_runs += currentFixturePlayerToUpdate.batting_runs;
                    }

                    if (currentFixturePlayerToUpdate.overs != null)
                    {
                        currentPlayerToUpdate.overs += currentFixturePlayerToUpdate.overs;
                    }

                    if (currentFixturePlayerToUpdate.maidens != null)
                    {
                        currentPlayerToUpdate.maidens += currentFixturePlayerToUpdate.maidens;
                    }

                    if (currentFixturePlayerToUpdate.bowling_runs != null)
                    {
                        currentPlayerToUpdate.bowling_runs += currentFixturePlayerToUpdate.bowling_runs;
                    }

                    if (currentFixturePlayerToUpdate.wickets != null)
                    {
                        currentPlayerToUpdate.wickets += currentFixturePlayerToUpdate.wickets;
                    }

                    if ((currentPlayerToUpdate.best_batting == null) && (currentFixturePlayerToUpdate.batting_runs != null))
                    {
                        currentPlayerToUpdate.best_batting = currentFixturePlayerToUpdate.batting_runs;
                    }

                    if ((currentPlayerToUpdate.best_batting != null) && (currentFixturePlayerToUpdate.batting_runs != null))
                    {
                        if (currentFixturePlayerToUpdate.batting_runs > currentPlayerToUpdate.best_batting)
                        {
                            currentPlayerToUpdate.best_batting = currentFixturePlayerToUpdate.batting_runs;
                        }
                    }

                    if ((currentFixturePlayerToUpdate.wickets != null) && (currentFixturePlayerToUpdate.bowling_runs != null))
                    {
                        if ((currentPlayerToUpdate.best_bowling_runs == null) && (currentPlayerToUpdate.best_bowling_wickets == null))
                        {
                            currentPlayerToUpdate.best_bowling_wickets = currentFixturePlayerToUpdate.wickets;
                            currentPlayerToUpdate.best_bowling_runs = currentFixturePlayerToUpdate.bowling_runs;
                        }
                        else if ((currentFixturePlayerToUpdate.wickets > currentPlayerToUpdate.best_bowling_wickets) && (currentFixturePlayerToUpdate.bowling_runs < currentPlayerToUpdate.best_bowling_runs))
                        {
                            currentPlayerToUpdate.best_bowling_wickets = currentFixturePlayerToUpdate.wickets;
                            currentPlayerToUpdate.best_bowling_runs = currentFixturePlayerToUpdate.bowling_runs;
                        }
                    }

                    db.ObjectStateManager.ChangeObjectState(currentPlayerToUpdate, EntityState.Modified);
                    db.SaveChanges();
                }
                else
                {
                    PlayerSeasonStatistic currentPlayerToAdd = new PlayerSeasonStatistic();
                    ClubFixturePlayer currentFixturePlayerToAdd = db.ClubFixturePlayers.Where(cfp => cfp.fixture_id == fixture.fixture_id && cfp.player_id == p).Single();

                    currentPlayerToAdd.player_id = (int)currentFixturePlayerToAdd.player_id;

                    currentPlayerToAdd.matches = 1;

                    if (currentFixturePlayerToAdd.how_out != 8)                          // Dismissal Id 8 = did not bat
                    {
                        currentPlayerToAdd.innings = 1;
                    }

                    if (currentFixturePlayerToAdd.how_out == 9)                          // Dismissal id 9 = not out
                    {
                        currentPlayerToAdd.not_out = 1;
                    }

                    if (currentFixturePlayerToAdd.batting_runs != null)
                    {
                        currentPlayerToAdd.batting_runs = (double)currentFixturePlayerToAdd.batting_runs;
                        currentPlayerToAdd.best_batting = currentPlayerToAdd.batting_runs;
                    }

                    if (currentFixturePlayerToAdd.overs != null)
                    {
                        currentPlayerToAdd.overs = (double)currentFixturePlayerToAdd.overs;
                    }

                    if (currentFixturePlayerToAdd.maidens != null)
                    {
                        currentPlayerToAdd.maidens = (double)currentFixturePlayerToAdd.maidens;
                    }

                    if (currentFixturePlayerToAdd.wickets != null)
                    {
                        currentPlayerToAdd.wickets = (double)currentFixturePlayerToAdd.wickets;
                        currentPlayerToAdd.best_bowling_wickets = currentPlayerToAdd.wickets;
                    }

                    if (currentFixturePlayerToAdd.bowling_runs != null)
                    {
                        currentPlayerToAdd.bowling_runs = (double)currentFixturePlayerToAdd.bowling_runs;
                        currentPlayerToAdd.best_bowling_runs = currentPlayerToAdd.bowling_runs;
                    }
                    
                    db.PlayerSeasonStatistics.AddObject(currentPlayerToAdd);
                    db.SaveChanges();
                }
            }
        }

    }

    public class PlayerSeasonStatisticsMetaData
    {
        [Display(Name = "Matches:")]
        public object matches;

        [Display(Name = "Innings:")]
        public object innings;

        [Display(Name = "Not Out:")]
        public object not_out;

        [Display(Name = "Runs:")]
        public object batting_runs;

        [Display(Name = "Overs:")]
        public object overs;

        [Display(Name = "Maidens:")]
        public object maidens;

        [Display(Name = "Runs:")]
        public object bowling_runs;

        [Display(Name = "Wickets:")]
        public object wickets;

    }

}